import { messages } from "share/common";
import React, { Component } from "react";
import { Form, Button, Spin } from "antd";
import HotelCardForm from "../components/hotel-card-form";
import HotelServer from "../hotel-server";
import { message } from "antd/lib/index";

const FormItem = Form.Item;

@Form.create()
class SetHotelSupplier extends Component {
  constructor(props) {
    super(props);
    this.state = {
      loading: false,
      hotelData: [], // 酒店供应商数据列表
    };
  }

  componentWillMount() {
    this.initData();
  }

  initData = () => {
    HotelServer(this)
      .getHotelSupplierData({ companyOid: this.props.companyOID })
      .then((res) => {
        if (res.data.body) {
          this.setState({ hotelData: res.data.body });
        } else if (res.data.message) {
          message.error(res.data.message);
        }
      });
  };

  onSaveSetting = () => {
    this.props.form.validateFieldsAndScroll((err, values) => {
      if (!err) {
        console.log(values);
        const params = {
          companyOid: this.props.companyOID,
          companyName: this.props.companyName,
          hotelVendorSigns: [],
        };
        for (let key in values) {
          params.hotelVendorSigns.push(values[key]);
        }
        HotelServer(this)
          .saveHotelSupplierData(params)
          .then((res) => {
            if (res.data.success) {
              this.initData();
              message.success(messages("supplier-1.key355") /*保存成功*/);
            } else {
              message.error(res.data.message);
            }
          });
      }
    });
  };

  render() {
    const { hotelData, loading } = this.state;
    const { getFieldDecorator } = this.props.form;
    return (
      <div className="airline-config-container">
        <Spin spinning={loading}>
          <p>
            <a>{messages("supplier-1.key488") /*目前只支持启用一个供应商*/}</a>
          </p>
          <Form>
            {hotelData.map((item, index) => {
              return (
                <FormItem>
                  {getFieldDecorator(`supplier${index}`, {
                    initialValue: item,
                  })(<HotelCardForm />)}
                </FormItem>
              );
            })}
          </Form>
          <br />
          <br />
          <br />
          <Button type="primary" onClick={this.onSaveSetting}>
            {messages("supplier-1.key356") /*保存配置*/}
          </Button>
          <br />
          <br />
          <br />
        </Spin>
      </div>
    );
  }
}

export default SetHotelSupplier;
